<?php	
	require_once KLEO_LIB_DIR.'core.lib.php';

	class admin extends adminmodel{
		var $dashboard_status = null;
		var $widget_places = array(
			1 => "Top",
			2 => "Head",
			3 => "Head-bottom",
			4 => "Center-up",
			5 => "Center-bottom",
			6 => "Footer",
			7 => "Right",
			8 => "Left",
			9 => "Sidebar"
		      );
		/* Call to the model Class */
		function __construct(){
			$this->index();
		}

		function load_the_pannel(&$buff){
		      if(isset($_GET['comment_action']) && isset($_GET['id'])){
			switch($_GET['comment_action']){
				case 'spam':
					$this->spam_comment((int)$_GET['id']);
				break;
				case 'delete':
					$this->delete_comment((int)$_GET['id']);
				break;
				case 'approve':
					$this->approve_comment((int)$_GET['id']);
				break;
			}
			$this->appredirect($_SERVER['HTTP_REFERER']);
		      }
		      if(isset($_POST['admin_message'])){
			if(kleocore::is_value('admin_message')){
				kleocore::set_core_value('admin_message', $this->escape_query_string($_POST['admin_message']));
			} else {
				kleocore::add_core_value('admin_message', $this->escape_query_string($_POST['admin_message']));
			}
		      }

			$this->tpl_load('dashboard.tpl',$buff);
			if(isset($_GET['file']) &&  !$this->is_remote_file($_GET['file'])){
 				$file = $_GET['file'];
			} else if(!isset($_GET['file'])){
				$file = 'dashboard_main.tpl';
			}
			
			if(file_exists($file) or file_exists(KLEO_MODULES_DIR.'admin/views/'.$file)){
			  $this->tpl_load($file,$dbuff);
			} else {
			  $this->appredirect('?module=admin');
			}

			$approved_comments_query = $this->query("SELECT * FROM ".KLEO_TABLE_PREFIX."comments WHERE costatus = 1");
			$not_approved_comments_query = $this->query("SELECT * FROM ".KLEO_TABLE_PREFIX."comments WHERE costatus = 2");
			$waiting_comments_query = $this->query("SELECT * FROM ".KLEO_TABLE_PREFIX."comments WHERE costatus = 0");
			$total_comments_query = $this->query("SELECT * FROM ".KLEO_TABLE_PREFIX."comments");

			// Compile the sidebar
			$this->make_sidebar_calls($dbuff,$dbuff,$this->sidebar_widgets);
			$this->make_admin_caption($dbuff,$dbuff,$page_caption);
			$this->make_breadcumbs($dbuff,$dbuff,$breadcrumbs);
			if(is_array($this->sidebar_widgets)){
				foreach($this->sidebar_widgets as $widget){
					if(file_exists('modules/admin/controllers/sidebar/'.$widget)){
						ob_start();
 						require('modules/admin/controllers/sidebar/'.$widget);
						$widgets .= ob_get_contents();
						ob_end_clean();
					}
				}
			}



			$uid = (int)$_SESSION['uid'];

			$query = $this->query("SELECT * FROM ".KLEO_TABLE_PREFIX."users_profiles, ".KLEO_TABLE_PREFIX."users WHERE ".KLEO_TABLE_PREFIX."users.uid = ".KLEO_TABLE_PREFIX."users_profiles.uid AND ".KLEO_TABLE_PREFIX."users.uid = $uid ORDER BY profileid DESC LIMIT 1");
			$this->profile_row = $this->fetch_array($query);

			if(!empty($this->profile_row['username'])){
				$username = $this->profile_row['username'];
			} else {
				$username = $this->get_username($_SESSION['uid']);
			}
			$this->tpl_set(array(
				"DASHBOARD_BREADCRUMBS" => $breadcrumbs,
				"DASHBOARD_CONTENT" => $dbuff,
				"DASHBOARD_SIDEBAR" => $widgets,
				"PAGE_CAPTION" => $page_caption,
				"USERNAME" => $username
			), false, $buff, $buff);


	 	 	$clean_name = substr($file,0,-4);
	 	 	if(file_exists(KLEO_MODULES_DIR.'admin/controllers/extras/'.$clean_name.'.php')){
				require_once KLEO_MODULES_DIR.'admin/controllers/extras/'.$clean_name.'.php';
				$extra_functions = new $clean_name();
				$extra_functions->make_extra($buff,&$buff);
				if(isset($extra_functions->dashboard_status)){
					$this->dashboard_status = $extra_functions->dashboard_status;
				}
			}


			if(file_exists(KLEO_SOURCE_DIR.'install') && !$_SESSION['dashboard_error']){
				$_SESSION['dashboard_error'] = L_INSTALL_FILES_EXISTS;
			}

			if(!isset($_SESSION['kleo_latest_version'])){
				$_SESSION['kleo_latest_version'] = substr(file_get_contents('http://kleophatra.org/services/version.txt'),0,-1);
			}
			if(str_replace(".","",$_SESSION['kleo_latest_version']) > str_replace(".","",KLEO_CURRENT_VERSION) && !isset($_SESSION['dashboard_status']) && !defined(KLEO_DEVELOPMENT_MODE)){
				$_SESSION['dashboard_status'] = array(0,L_PLEASE_UPGRADE);
			}
			if(isset($_SESSION['dashboard_status'])){
				if($_SESSION['dashboard_status'][0] == 0){
					$this->tpl_set("DASHBOARD_STATUS", '', $buff, $buff);
					$_SESSION['dashboard_status'][0] = 1;
					$this->appreload();
				} else if($_SESSION['dashboard_status'][0] == 1){
					$this->tpl_set("DASHBOARD_STATUS", '<div onmouseover="Effect.Fade(this);" id="status">'.$_SESSION['dashboard_status'][1].'</div>', $buff, $buff);
					unset($_SESSION['dashboard_status']);
				}
			}
			if(isset($_SESSION['dashboard_error'])){
				$this->tpl_set("DASHBOARD_STATUS", '<div onclick="Effect.Fade(this);" id="status_error">'.$_SESSION['dashboard_error'].'</div>', $buff, $buff);
				unset($_SESSION['dashboard_error']);
			}
			if(!isset($_SESSION['dashboard_status']) && !isset($_SESSION['dashboard_error'])){
				$this->tpl_set("DASHBOARD_STATUS", '', $buff, $buff);
			}
			if(!isset($_GET['file'])){
				$_GET['file'] = null;
			}
			if(!isset($_SESSION['admin_title'.$_GET['file']])){
				$admin_title = L_MAIN_DASHBOARD;
			} else {
				$admin_title = $_SESSION['admin_title'.$_GET['file']];
			}

			$this->tpl_set(array(
				"ADMIN_PRE_TITLE" => L_ADMIN_PRE_TITLE,
				"ADMIN_TITLE" => $admin_title
			), false, $buff, $buff);

			$this->compile($buff,$buff);
		}

		function approve_comment($id){
			$this->query("UPDATE `".KLEO_TABLE_PREFIX."comments` SET `costatus` = '1' WHERE `coid` =$id LIMIT 1 ;");
		}
		function spam_comment($id){
			$this->query("UPDATE `".KLEO_TABLE_PREFIX."comments` SET `costatus` = '2' WHERE `coid` =$id LIMIT 1 ;");
		}
		function delete_comment($id){
			$this->query("DELETE FROM `".KLEO_TABLE_PREFIX."comments` WHERE `coid` = $id LIMIT 1");
    		}
		function is_post($id){
			$query = $this->query("SELECT * FROM ".KLEO_TABLE_PREFIX."blog WHERE id = $id");
			if($this->num_rows($query) !== 0){
				return true;
			} else {
				return false;
			}
		}
	}

?> 
